Const ks_CIRCUIT_FILE_NAME$= "circuit.txt"
Const ks_WAYPOINT_FILE_NAME$= "waypoint.txt"
Const ki_MAX_TILE = 65535



Type TRaceData
	Field nb_row
	Field nb_col
	Field TileData$[ki_MAX_TILE]
	Field WayPointData$[ki_MAX_TILE]	
	Field nb_Waypoint 
	Field Curr_Race$
End Type 



Function TRaceData_Create.TRaceData(p_Race$)
	Local l_d.TRaceData = New TRaceData
	l_d\Curr_Race$ = ".\level\"+p_Race$

	ChangeDir l_d\Curr_Race$	
	l_d\nb_row= fi_get_nb_row( ks_CIRCUIT_FILE_NAME$ )
	l_d\nb_col= fi_get_nb_col( ks_CIRCUIT_FILE_NAME$ )
	l_d\nb_Waypoint = 0
	l_d=TRaceData_LoadTile( l_d )	
	l_d=TRaceData_LoadWayPoint( l_d )	
	Return l_d
End Function 



Function TRaceData_LoadTile.TRaceData( p_rd.TRaceData)
	
	Local li_file=ReadFile(ks_CIRCUIT_FILE_NAME$)					
	Local li_TileIndex = 0
	Local li_WordIndex = 0 ; betweeb 1..fi_get_nb_col
	Local ls$ =""
	If li_file > 0 ; file found
		While Not Eof(li_file)
			ls$=ReadLine(li_file)		
			
			li_WordIndex = 1
			For li_col=0 To p_rd\nb_col-1	
				p_rd\TileData$[li_TileIndex]=gettok$(ls$,li_WordIndex,",")
				li_TileIndex=li_TileIndex+1
				li_WordIndex=li_WordIndex+1					
			Next
		Wend  
		CloseFile( li_file ) 
	Else 
		RuntimeError "File name " + file_name$ + " is not found. Load race description impossible."
	EndIf 
    Return p_rd
End Function 



Function TRaceData_LoadWayPoint.TRaceData( p_rd.TRaceData)
	
	Local li_file=ReadFile(ks_WAYPOINT_FILE_NAME$)					
	Local li_TileIndex = 0
	Local li_WordIndex = 0 ; betweeb 1..fi_get_nb_col
	Local ls$ =""
	If li_file > 0 ; file found
		While Not Eof(li_file)
			ls$=ReadLine(li_file)		
				li_WordIndex = 1
				For li_col=0 To p_rd\nb_col-1	
					p_rd\WayPointData$[li_TileIndex]=gettok$(ls$,li_WordIndex,",")
					If p_rd\WayPointData$[li_TileIndex] <> "0" Then p_rd\nb_Waypoint = p_rd\nb_Waypoint + 1 
					li_TileIndex=li_TileIndex+1
					li_WordIndex=li_WordIndex+1					
				Next
		Wend  
		CloseFile( li_file ) 
	Else 
		RuntimeError "File name " + file_name$ + " is not found. Load way point description impossible."
	EndIf 
	 
    Return p_rd
End Function 



Function fi_get_nb_row( file_name$ )
	Local li_nb_row=0

	If file_name <> "" Then
		Local li_file=ReadFile(file_name$)					
		If li_file > 0 ; file found
			While Not Eof(li_file)
				ReadLine(li_file)		
				li_nb_row=li_nb_row+1
			Wend  
			CloseFile( li_file ) 
		Else 
			RuntimeError "File name " + file_name$ + " is not found."
		EndIf 
	End If 
	
	If li_nb_row = 0 
		RuntimeError "File name " + file_name$ + " must have a content."
	End If
	
	Return li_nb_row
End Function 



Function fi_get_nb_col( file_name$ )
	Local li_nb_col=0

	If file_name <> "" Then
		Local li_file=ReadFile(file_name$)		
		If li_file > 0 ; file found	
			Local ls_line$=ReadLine(li_file)		
			CloseFile( li_file ) 		
			If Len(ls_line) > 0 Then ; line must have something
				For t = 1 To Len(ls_line$) 
					If  Mid$(ls_line$,t,1) = "," Then li_nb_col=li_nb_col+1
				Next 		
			End If
		Else 
			RuntimeError "File name " + file_name$ + " is not found."					
		EndIf 		
	End If 

	If li_nb_col = 0 
		RuntimeError "File name " + file_name$ + " must have a content."
	End If
		
	Return (li_nb_col+1) ; 1 the last character have no ","
End Function 



Function TRaceData_GetTile$( p_rd.TRaceData, pi_Row, pi_Col )
	If pi_Row > p_rd\nb_row Then RuntimeError "TRaceData_GetTile, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Col > p_rd\nb_col Then RuntimeError "TRaceData_GetTile, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Row < 0 Then RuntimeError "TRaceData_GetTile, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Col < 0 Then RuntimeError "TRaceData_GetTile, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"	
	Return p_rd\TileData$[(pi_Row*p_rd\nb_col)+pi_col]
End Function



Function TRaceData_GetWp$( p_rd.TRaceData, pi_Row, pi_Col )
	If pi_Row > p_rd\nb_row Then RuntimeError "TRaceData_GetWp, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Col > p_rd\nb_col Then RuntimeError "TRaceData_GetWp, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Row < 0 Then RuntimeError "TRaceData_GetWp, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"
	If pi_Col < 0 Then RuntimeError "TRaceData_GetWp, Out of Range (Row="+pi_Row+",Col="+pi_Col+")"	

	Return p_rd\WayPointData$[(pi_Row*p_rd\nb_col)+pi_col]
End Function


;Local l_rd.TRaceData = Null 
;l_rd=TRaceData_Create("race01")
;Print "After"
;ls$=""
;li=0
;For li_row=0 To l_rd\nb_row-1 				
;	ls$=""
;	For li_col=0 To l_rd\nb_col-1								
;		ls$=ls$+TRaceData_GetWp(l_rd, li_row, li_col ) +","
;		li=li+1
;	Next
;	Print ls$
;Next			
 
;WaitKey
;End 